/*
 * 创建日期 2010-5-15
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.compile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import  org.fantasy.common.cache.Cache;
import  org.fantasy.common.util.Constants;
import  org.fantasy.common.util.StringUtil;
import  org.fantasy.common.util.WebUtil;

/**
 *  编译工具
 * @author: 王文成
 * @version: 1.0
 * @since 2010-5-15
 */
public class CompileUtil {

    private static final Log log = LogFactory.getLog(CompileUtil.class);

    private static Map<String, Object> PARAM_MAP = new HashMap<String, Object>();

    /**
     * 编译指定Service
     * 
     * @param request
     * @param sqlName
     * @throws Exception
     */
    public static void compile(HttpServletRequest request, String sqlName) throws Exception {
        CompileCenter compileCenter = (CompileCenter) getBean(request, "compileCenter");
        Cache serviceCache = (Cache) getBean(request, "sqlConfigCache");
        serviceCache.reloadCache();
        List<String> names = Arrays.asList(sqlName.split("[\r\n;,]"));
        compileCenter.reload(names);
        for (String name : names) {
            if (StringUtil.isValid(name)) {
                compileCenter.compile(name.trim(), PARAM_MAP);
            }
        }
    }

    /**
     * 编译全部Service
     * 
     * @param request
     * @throws Exception
     */
    @SuppressWarnings("all")
    public static void compileAll(HttpServletRequest request) throws Exception {
        CompileCenter compileCenter = (CompileCenter) getBean(request, "compileCenter");
        Cache serviceCache = (Cache) getBean(request, "sqlConfigCache");
        serviceCache.reloadCache();
        compileCenter.reloadAll();
        Map serviceMap = (Map)serviceCache.getCache();
        List<String> services = new ArrayList<String>(serviceMap.keySet());
        log.info("Compile [" + services.size() + "] Services Start ........................!");
        for (String serviceName : services) {
            if (!(serviceName.endsWith(Constants.XML_SERVICE_TYPE) || serviceName.endsWith(Constants.XML_SERVICE_BEAN) || serviceName
                    .endsWith(Constants.XML_SERVICE_RESULT_TYPE))) {
                try {
                    compileCenter.compile(serviceName, PARAM_MAP);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.info("Compile Error[" + serviceName + "]");
                    log.error(e);
                }
            }
        }
        log.error("Compile All Complete .....................!");
    }

    /**
     * 根据文件编译Service
     * 
     * @param request
     * @param fileName
     * @throws Exception
     */
    public static void compileFile(HttpServletRequest request, String fileName) throws Exception {
        
    }

    /**
     * 取得Service的配置缓存
     * 
     * @param request
     * @return
     */
    public static Object getBean(HttpServletRequest request, String id) {
        return WebUtil.getBean(request, id);
    }

}
